package org.example.utils;


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.time.LocalDate;
import java.util.Properties;

public class EmailUtil {
    // 邮件服务器配置
    private static final String HOST = "smtp.qq.com";
    private static final int PORT = 587;
    private static final String USERNAME = "3324094736@qq.com";
    private static final String PASSWORD = "acbfcgduuniqchfg";

    public static void sendVerificationCode(String toEmail, String code) throws MessagingException {
        // 配置邮件服务器属性
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true"); // 使用TLS加密

        // 创建会话
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(USERNAME, PASSWORD);
            }
        });

        // 创建邮件消息
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(USERNAME));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject("投票系统 - 验证码");

        // 邮件内容（支持HTML）
        String content = "<html><body>" +
                "<h3>您的验证码是：</h3>" +
                "<p style=\"font-size:24px;font-weight:bold;\">" + code + "</p>" +
                "<p>该验证码5分钟内有效，请尽快完成验证。</p>" +
                "</body></html>";

        message.setContent(content, "text/html; charset=UTF-8");

        // 发送邮件
        Transport.send(message);
    }

    public static void contantAdmin(String name, String subject, String email, String content) throws MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true"); // 使用TLS加密

        // 创建会话
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(USERNAME, PASSWORD);
            }
        });
        // 创建邮件消息
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(USERNAME));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("3324094736@qq.com"));
        message.setSubject("投票系统 - 验证码");
        // 邮件内容（支持HTML）
        String body = "<!DOCTYPE html>\n" +
                "<html lang=\"zh-CN\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>投票系统 - 用户反馈</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div>\n" +
                "        <h1>投票系统 - 用户反馈</h1>\n" +
                "        \n" +
                "        <p>管理员，您好：</p>\n" +
                "        \n" +
                "        <p>收到一位用户的反馈信息，请查收：</p>\n" +
                "        \n" +
                "        <div>\n" +
                "            <p><strong>姓名：</strong> " + name + "</p>\n" +
                "            <p><strong>邮箱：</strong>" + email + " </p>\n" +
                "            <p><strong>主题：</strong> " + subject + "</p>\n" +
                "            <p><strong>时间：</strong> " + LocalDate.now() + "</p>\n" +
                "        </div>\n" +
                "        \n" +
                "        <div>\n" +
                "            " + content + "  \n" +
                "        </div>\n" +
                "        \n" +
                "        <p>请及时处理用户反馈，并在系统中标记处理状态。</p>\n" +
                "        \n" +
                "        \n" +
                "        <hr>\n" +
                "        \n" +
                "        <p>此邮件由系统自动发送，请勿直接回复。</p>\n" +
                "        \n" +
                "        <p>投票系统管理平台</p>\n" +
                "        <p>工作时间：周一至周五 9:00-18:00</p>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";

        message.setContent(body, "text/html; charset=UTF-8");

        // 发送邮件
        Transport.send(message);
    }
}